package com.ruoyi.web.controller.market.member;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.MemberInfo;
import com.ruoyi.db.model.excle.MemberInfoExcel;
import com.ruoyi.db.model.param.ChannelInfoParam;
import com.ruoyi.db.model.param.MemberInfoParam;
import com.ruoyi.extend.service.IChannelInfoService;
import com.ruoyi.member.service.IMemberInfoService;
import com.ruoyi.member.service.IMemberLoginsessionService;
import com.ruoyi.product.service.IProductProfitAddupService;
import com.ruoyi.web.service.IMemberInfoHandleService;
import com.ruoyi.web.wapper.MemberInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 用户列表Controller
 * 
 * @author ruoyi
 * @date 2022-08-02
 */
@Controller
@RequestMapping("/member/info")
public class MemberInfoController extends BaseController
{
    private String prefix = "member/info";

    @Autowired
    private IMemberInfoService memberInfoService;
    @Autowired
    private IChannelInfoService channelInfoService;
    @Autowired
    IMemberInfoHandleService memberInfoHandleService;
    @Autowired
    IProductProfitAddupService productProfitAddupService;

    @Autowired
    private IMemberLoginsessionService memberLoginsessionService;

    @RequiresPermissions("member:info:view")
    @GetMapping()
    public String info(ModelMap mmap)
    {
        // 查询所有渠道的渠道信息
        ChannelInfoParam channelInfoParam = new ChannelInfoParam();
        // 查询渠道号信息
        List<Map<String, String>> channelCodeList = channelInfoService.selectSimpleMapInfo(channelInfoParam);
        mmap.put("channelCodeList", channelCodeList);
        return prefix + "/info";
    }

    /**
     * 查询用户列表列表
     */
    @RequiresPermissions("member:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MemberInfoParam memberInfoParam)
    {
        startPage();
        List<Map<String, Object>> mapList = memberInfoService.selectMemberInfoListMap(memberInfoParam);
        mapList = new MemberInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出用户列表列表
     */
    @Log(title = "用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MemberInfoParam memberInfoParam)
    {
        List<MemberInfoExcel> list = memberInfoService.selectMemberInfoList(memberInfoParam);
        ExcelUtil<MemberInfoExcel> util = new ExcelUtil<MemberInfoExcel>(MemberInfoExcel.class);
        return util.exportExcel(list, "用户列表数据");
    }

    /**
     * 新增用户列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户列表
     */
    @RequiresPermissions("member:info:add")
    @Log(title = "用户列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MemberInfo memberInfo)
    {
        return toAjax(memberInfoService.insertMemberInfo(memberInfo));
    }

    /**
     * 修改用户列表
     */
//    @RequiresPermissions("member:info:edit")
//    @GetMapping("/edit/{uid}")
//    public String edit(@PathVariable("uid") String uid, ModelMap mmap)
//    {
//        MemberInfo memberInfo = memberInfoService.selectMemberInfoByUid(uid);
//        mmap.put("memberInfo", memberInfo);
//        return prefix + "/edit";
//    }

    /**
     * 修改保存用户列表
     */
    @RequiresPermissions("member:info:edit")
    @Log(title = "用户列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MemberInfo memberInfo)
    {
        return toAjax(memberInfoService.updateMemberInfo(memberInfo));
    }

    /**
     * 删除用户列表
     */
    @RequiresPermissions("member:info:remove")
    @Log(title = "用户列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(memberInfoService.deleteMemberInfoByUids(ids));
    }


    @GetMapping("/view/{uid}")
    public String view(@PathVariable("uid") Long uid, ModelMap mmap)
    {
        JSONObject userInfo = memberInfoHandleService.getMemberInfoDetail(uid);
        userInfo.put("mobile", "xxxxxx");
        mmap.put("userInfo", userInfo);

        // 查询用户推送信息
        List<Map<String, Object>> pushResultList = memberInfoService.selectPushResultList(uid);
        mmap.put("pushResultList", pushResultList);

        //查询用户点击的产品信息
        List<Map<String, Object>> ProfitaddupList = productProfitAddupService.selectProfitaddup(uid);
        mmap.put("profitaddup", ProfitaddupList);

        //查询用户的登录信息
        List<Map<String, Object>> memberLoginsession = memberLoginsessionService.selectMemberLoginsessionByUid(uid);
        mmap.put("Loginsession", memberLoginsession);

        return prefix + "/view";
    }

    @RequiresPermissions("member:info:resetExt")
    @GetMapping("/change")
    public String change(ModelMap mmap)
    {
        return prefix + "/changeExt";
    }

    /**
     * 修改保存用户列表
     */
    @RequiresPermissions("member:info:resetExt")
    @PostMapping("/changeExt")
    @ResponseBody
    public AjaxResult changeExt(MemberInfo memberInfo)
    {
        return toAjax(memberInfoService.updateExtByMobile(memberInfo.getMobile()));
    }

    @RequiresPermissions("member:info:queryMobile")
    @GetMapping("/queryMobile")
    public String queryMobile(ModelMap mmap)
    {
        return prefix + "/queryMobile";
    }

    /**
     * 修改保存用户列表
     */
    @RequiresPermissions("member:info:queryMobile")
    @PostMapping("/queryMobileData")
    @ResponseBody
    @Log(title = "用户列表手机号查询", businessType = BusinessType.OTHER)
    public AjaxResult queryMobileData(MemberInfo memberInfo)
    {

        SysUser sysUser = getSysUser();
        String redisKey = QUERY_MOBILE_COUNT+sysUser.getUserId();
        String number = stringRedisTemplate.opsForValue().get(redisKey);
        if (ObjectUtil.isNotEmpty(number) && Integer.valueOf(number) > 60){
            return AjaxResult.error("查询错误");
        }

        MemberInfo memberInfoData = memberInfoService.selectMemberInfoByUid(String.valueOf(memberInfo.getUid()));

        String mobile = "没有查询到数据";
        if (ObjectUtil.isNotEmpty(memberInfoData)){
            mobile = memberInfoData.getMobile();
        }
        JSONObject resultData = new JSONObject();
        resultData.put("mobile", mobile);

        if (stringRedisTemplate.hasKey(redisKey)){
            stringRedisTemplate.opsForValue().increment(redisKey);
        }else{
            stringRedisTemplate.opsForValue().setIfAbsent(redisKey, "1", 1L, TimeUnit.HOURS);
        }
        return AjaxResult.success(resultData);
    }

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private static final String QUERY_MOBILE_COUNT = "QUERY:MOBILE:COUNT:";


}
